/**
 * Handles making an API call to a server running Concerto
 * 
 * @package Torchd Framework
 * @subpackage Concerto
 * @author Jason Raede <jason@torchdm.com>
 * @param object params		Javascript object of parameters for the API call
 */
torchd.concerto.apiCall = function(params) {
	this.init(params);
};

torchd.concerto.apiCall.defaultParams = {
	/**
	 * HTTP request type. Only supports GET or POST because DELETE and PUT are not reliably supported
	 * The DELETE request can be mimicked by setting data.requestType to "DELETE" on a POST request
	 */
	requestType:'GET',
	
	/**
	 * Data to send to the server
	 */
	data:{},
	
	/**
	 * Optional action to take during the call, like showing an ajax loading gif
	 */
	duringLoad:false,
	
	/**
	 * Text to display in the ajax loader dialog if duringLoad is set to 'ajax-loader-dialog'
	 */
	ajaxLoadingText:'Loading.',
	
	/**
	 * Call identifier for handling in the view controller
	 */
	callId:'call',
	
	/**
	 * Keep this as true if this script is run on a different server than the
	 * Concerto server you are trying to talk to. If they are on the same
	 * server, there are other security measures taken so there is no secret
	 * API key required
	 */
	useSender:true
};

torchd.concerto.apiCall.prototype.init = function(params) {
	this.params = $.extend({}, torchd.concerto.apiCall.defaultParams, params);
};

/**
 * Takes the duringLoad argument and creates functions based on its value
 *
 * @param string duringLoad
 * @param string ajaxLoadingText
 * @return object Object with beforeSend and success functions
 */
torchd.concerto.apiCall.parseDuringLoad = function(duringLoad, ajaxLoadingText) {
	var functions = {};
	switch(duringLoad) {
		case 'ajax-loader-dialog':
			functions.beforeSend = function() {
				torchd.ui.showDialog({
					type:'ajax-loader',
					title:ajaxLoadingText
				});
			};
			functions.success = function() {
				torchd.ui.closeDialog(ajaxLoadingText);
			};
			break;
		default:
			functions.beforeSend = function(){};
			functions.success = function(){};
	}
	return functions;
};	

torchd.concerto.apiCall.prototype.execute = function() {
	var url = '';
	if(this.params.useSender) url = 'apiSender.php';
	else url = concerto.rootUrl + '?view=api';
	var functions = torchd.concerto.apiCall.parseDuringLoad(this.params.duringLoad, this.params.ajaxLoadingText);
	$.ajax({
		url:url
		type:this.params.requestType,
		data:this.params.data,
		beforeSend
		
	});
};

torchd.concerto.apiCall.prototype.handleData = function() {

};